<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Settle;
use App\Http\Model\FinanceKk;

class RoomBankCharmMake extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'RoomBankCharmMake';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '重新生成房间人气日榜周榜';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    protected $financeCharmStreamTable = 'finance_charm_stream';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $roomIds = [];

        $now = time();
        //今日
        $ymdNow = date("Ymd", $now);
        //周一
        $weekSt = date("Ymd", strtotime('this week Monday'));
        //周日
        $weekEt = date("Ymd", strtotime('this week Sunday'));

        //本周有流水的房间
        if(empty($roomIds)) {
            $list = DB::table($this->financeCharmStreamTable)
                ->where('change_time', '>=', date("Y-m-d 00:00:00", strtotime($weekSt)))
                ->where('change_time', '<=', date("Y-m-d 23:59:59", strtotime($weekEt)))
                ->where('room_id', '>', 0)
                ->select('room_id')
                ->distinct()
                ->get();
            $list = objToArray($list);
            if(!empty($list)) {
                $roomIds = array_column($list, 'room_id');
            }
        }

        foreach($roomIds as $roomId) {
            $this->_roomMakeRank($roomId, $ymdNow, $weekSt, $weekEt);
        }
    }

    //某个房间重新生成房间人气日榜周榜
    public function _roomMakeRank($roomId, $ymdNow, $weekSt, $weekEt)
    {
        do {
            //日榜
            $cacheKey = "FINANCE:CHARTS:ROOM:USER:CHARM:DAY:".$roomId.':'.$ymdNow;
            Redis::DEL($cacheKey);
            $list = DB::table($this->financeCharmStreamTable)
                ->where('room_id', $roomId)
                ->where('change_time', '>=', date("Y-m-d 00:00:00", strtotime($ymdNow)))
                ->where('change_time', '<=', date("Y-m-d 23:59:59", strtotime($ymdNow)))
                ->groupBy('user_id')
                ->selectRaw('user_id,sum(charm_value) as num')
                ->get();
            $list = objToArray($list);
            foreach($list as $val) {
                Redis::ZADD($cacheKey, $val['num'], $val['user_id']);
                Redis::EXPIRE($cacheKey, 2 * 24 * 3600);
            }

            //周榜
            $cacheKey = "FINANCE:CHARTS:ROOM:USER:CHARM:WEEK:".$roomId.':'.$weekSt.':'.$weekEt;
            Redis::DEL($cacheKey);
            $list = DB::table($this->financeCharmStreamTable)
                ->where('room_id', $roomId)
                ->where('change_time', '>=', date("Y-m-d 00:00:00", strtotime($weekSt)))
                ->where('change_time', '<=', date("Y-m-d 23:59:59", strtotime($weekEt)))
                ->groupBy('user_id')
                ->selectRaw('user_id,sum(charm_value) as num')
                ->get();
            $list = objToArray($list);
            foreach($list as $val) {
                Redis::ZADD($cacheKey, $val['num'], $val['user_id']);
                Redis::EXPIRE($cacheKey, 2 * 7 * 24 * 3600);
            }

            echo $roomId." ok \r\n";
        } while (false);

    }


}